package hfu.pm.bll;

import java.util.List;

import com.googlecode.objectify.Key;

import hfu.pm.domain.DAO;
import hfu.pm.domain.DAOT;
import hfu.pm.domain.DAOT.Transactable;
import hfu.pm.entities.Account;
import hfu.pm.entities.Contact;
import hfu.pm.entities.Invitation;
import hfu.pm.entities.Room;

public class ContactLogic {

	static DAO dao = new DAO();

	// check if "friend" is in contact list of "owner"
	public static boolean isInContactList(Account owner, Account friend) {
		Iterable<Contact> tmp = dao.ofy().query(Contact.class)
				.filter("account", owner).filter("friend", friend)
				.filter("active", Boolean.TRUE);
		return tmp.iterator().hasNext();
	}

	// add friend
	public static void addToContactList(Account acc1, Account acc2) {
		if (!isInContactList(acc1, acc2)) {
			final Contact contact1 = new Contact();
			contact1.setAccount(new Key<Account>(Account.class, acc1.getId()));
			contact1.setFriend(new Key<Account>(Account.class, acc2.getId()));
			contact1.setActive(true);

			if (acc1.equals(acc2)) {
				contact1.setFriendName(acc2.getUser().getNickname());
				dao.ofy().put(contact1);
			} else {
				contact1.setFriendName(getContactOf(acc2, acc2).getFriendName());

				final Contact contact2 = new Contact();
				contact2.setAccount(new Key<Account>(Account.class, acc2
						.getId()));
				contact2.setFriend(new Key<Account>(Account.class, acc1.getId()));
				contact2.setActive(true);

				contact2.setFriendName(getContactOf(acc1, acc1).getFriendName());

				DAOT.runInTransaction(new Transactable() {

					@Override
					public void run(DAOT daot) {
						dao.ofy().put(contact1);
						dao.ofy().put(contact2);
					}
				});
			}

		}
	}

	// get Contact object of "owner" and "friend"
	public static Contact getContactOf(Account owner, Account friend) {

		return dao.ofy().query(Contact.class).filter("account", owner)
				.filter("friend", friend).get();
	}

	// remove a contact from ContactList
	public static void removeFromContactList(Account owner, Account friend) {
		Contact contact = getContactOf(owner, friend);
		if (contact != null) {
			contact.setActive(false);
			contact.setTimestamp();
			dao.ofy().put(contact);
		}
	}

	// get ContactList
	public static List<Contact> getContactList(Account owner) {
		return dao.ofy().query(Contact.class).filter("account", owner)
				.filter("active", Boolean.TRUE).filter("friend !=", owner)
				.list();
	}

	// get an account from its key
	public static Account getAccount(String key) {
		Key<Account> k = new Key<Account>(key);
		return dao.ofy().get(k);
	}

	// get owner from a contact
	public static Account getOwnerOfContact(Contact contact) {
		return dao.ofy().get(contact.getAccount());
	}

	// get friend from a contact
	public static Account getFriendOfContact(Contact contact) {
		return dao.ofy().get(contact.getFriend());
	}

	// get an contact from its key
	public static Contact getContact(String key) {
		Key<Contact> k = new Key<Contact>(key);
		return dao.ofy().get(k);
	}

}
